package com.myk.game.gobangchess.dates;

import com.google.gson.Gson;
import com.myk.game.gobangchess.Account;
import com.myk.game.gobangchess.Player;
import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.beans.PlayerPlayingBean;
import com.myk.game.gobangchess.constants.PathCost;
import com.myk.game.gobangchess.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class PlayingData {

    /**
     * 获取对弈记录信息
     */
    public static String getPlayingDetail(Account account) {
        Gson gson = new Gson();
        String playingFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE;
        if (FileUtils.isFileExists(playingFile)) {
            String playingBeanJson = FileUtils.readFile2String(playingFile);
            PlayerPlayingBean playingBean = gson.fromJson(playingBeanJson, PlayerPlayingBean.class);
            if (playingBean != null) {
                String playingMap;
                StringBuilder sb = new StringBuilder();
                for (PlayerPlayingBean.ChessPlayingBean chessPlayingBean : playingBean.getChessPlayingList()) {
                    sb.append(chessPlayingBean.getChessName()).append(",").append(chessPlayingBean.getPlayingCount()).append(",").append(chessPlayingBean.getWinCount()).append(";");
                }
                String s = sb.toString();
                if (s.isEmpty()) {
                    playingMap = "-1";
                }
                else {
                    playingMap = s.substring(0, s.length() - 1);
                }
                return account.getIdCode() + "&" + account.getNickName() + "&" + playingMap;
            }
            else {
                return account.getIdCode() + "&" + account.getNickName() + "&" + "-1";
            }
        }
        else {
            return account.getIdCode() + "&" + account.getNickName() + "&" + "-1";
        }
    }

    /**
     * 获取对弈次数最多的棋
     */
    public static String getMaxPlayingChess(Account account) {
        Gson gson = new Gson();
        String playingFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE;
        if (FileUtils.isFileExists(playingFile)) {
            String playingBeanJson = FileUtils.readFile2String(playingFile);
            PlayerPlayingBean playingBean = gson.fromJson(playingBeanJson, PlayerPlayingBean.class);
            if (playingBean != null && playingBean.getChessPlayingList().size() > 0) {
                PlayerPlayingBean.ChessPlayingBean maxPlayingBean = playingBean.getChessPlayingList().get(0);
                for (PlayerPlayingBean.ChessPlayingBean chessPlayingBean : playingBean.getChessPlayingList()) {
                    if (chessPlayingBean.getPlayingCount() >= maxPlayingBean.getPlayingCount()) {
                        maxPlayingBean = chessPlayingBean;
                    }
                }
                return maxPlayingBean.getChessName();
            }
            else {
                return "";
            }
        }
        else {
            return "";
        }
    }

    /**
     * 更新玩家对弈信息
     */
    public static void updatePlayingInfo(Account account, Room room, Player opponent, boolean isWin) {
        String chessType = room.getChessName();
        Gson gson = new Gson();
        String playingFile = PathCost.DATA_ACCOUNT_GROUP_DIR + account.getIdCode() + "_" + account.getNickName() + "/" + PathCost.ACCOUNT_PLAYING_FILE; //获取当前用户对弈信息文件
        if (FileUtils.isFileExists(playingFile)) {
            String playingData = FileUtils.readFile2String(playingFile);
            PlayerPlayingBean oldPlayingBean = gson.fromJson(playingData, PlayerPlayingBean.class); //获取本地保存的对弈信息数据
            List<PlayerPlayingBean.ChessPlayingBean> chessPlayingBeanList = oldPlayingBean.getChessPlayingList();
            PlayerPlayingBean.ChessPlayingBean chessPlayingBean = null;
            for (PlayerPlayingBean.ChessPlayingBean playingBean : chessPlayingBeanList) {
                if (playingBean.getChessName().equals(chessType)) {
                    chessPlayingBean = playingBean;
                }
            }
            if (chessPlayingBean != null) {
                chessPlayingBean.setPlayingCount(chessPlayingBean.getPlayingCount() + 1);
                chessPlayingBean.setWinCount(chessPlayingBean.getWinCount() + (isWin ? 1 : 0));
                chessPlayingBean.setRecentOpponent(opponent.getNickNameOrTemp());
                chessPlayingBean.setRecentTime(System.currentTimeMillis());
            }
            else {
                chessPlayingBean = new PlayerPlayingBean.ChessPlayingBean();
                chessPlayingBean.setChessName(chessType);
                chessPlayingBean.setPlayingCount(1);
                chessPlayingBean.setWinCount(isWin ? 1 : 0);
                chessPlayingBean.setRecentOpponent(opponent.getNickNameOrTemp());
                chessPlayingBean.setRecentTime(System.currentTimeMillis());
                chessPlayingBeanList.add(chessPlayingBean);
            }
            PlayerPlayingBean newPlayingBean = new PlayerPlayingBean();
            newPlayingBean.setChessPlayingList(chessPlayingBeanList);
            String json = gson.toJson(newPlayingBean);
            FileUtils.writeFileFromString(playingFile, json);  //更新登录信息文件
        }
        else { //若登录信息文件不存在，创建文件并且存初始化数据
            PlayerPlayingBean newPlayingBean = new PlayerPlayingBean();
            List<PlayerPlayingBean.ChessPlayingBean> chessPlayingBeanList = new ArrayList<>();
            PlayerPlayingBean.ChessPlayingBean chessPlayingBean = new PlayerPlayingBean.ChessPlayingBean();
            chessPlayingBean.setChessName(chessType);
            chessPlayingBean.setPlayingCount(1);
            chessPlayingBean.setWinCount(isWin ? 1 : 0);
            chessPlayingBean.setRecentOpponent(opponent.getNickNameOrTemp());
            chessPlayingBean.setRecentTime(System.currentTimeMillis());
            chessPlayingBeanList.add(chessPlayingBean);
            newPlayingBean.setChessPlayingList(chessPlayingBeanList);
            String json = gson.toJson(newPlayingBean);
            FileUtils.createOrExistsFile(playingFile);
            FileUtils.writeFileFromString(playingFile, json);
        }
    }

    /**
     * ！！！ 清除全局所有玩家对弈数据
     */
    public static void cleanPlayingData() {
        String accountsPath = PathCost.DATA_ACCOUNT_GROUP_DIR;
        List<File> accountDirList = FileUtils.listFilesInDir(accountsPath);
        if (accountDirList.size() > 0) {
            for (File file : accountDirList) {
                String playingFile = accountsPath + file.getName() + "/" + "playing.txt";
                if (FileUtils.isFileExists(playingFile)) {
                    FileUtils.delete(playingFile);
                }
            }
        }
    }
}
